<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Model extends CI_Model {

    protected $_data = array();
    protected $_idFieldName = null;
    protected $_entityName = null;
    protected $_resource = null;
    
    public function __construct() {
    	parent::__construct();
    }
    
    public function setResource($resource) {
    	$this->_resource = $resource;
    }

    protected function getData($key) {
		return isset($this->_data[$key]) ? $this->_data[$key] : null;
    }
	
    protected function setData($key, $value=null) {
		$this->_data[$key] = $value;
        return $this;
    }

    public function __call($method, $args) {
        $key = $this->_underscore(substr($method,3));
    	
        switch (substr($method, 0, 3)) {
            case 'get' :
                $data = $this->getData($key, isset($args[0]) ? $args[0] : null);
                return $data;

            case 'set' :
                $result = $this->setData($key, isset($args[0]) ? $args[0] : null);
                return $result;

            case 'has' :
                return isset($this->_data[$key]);
        }
        throw new Exception("Invalid method ".get_class($this)."::".$method."(".print_r($args,1).")");
    }

    protected function _underscore($name) {
        $result = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name));
        return $result;
    }

    public function getIdFieldName() {
        return $this->_idFieldName;
    }

    public function getId() {
        if ($this->getIdFieldName()) {
            return $this->_getData($this->getIdFieldName());
        }
        return $this->_getData('id');
    }
	
    protected function addData($data) {
    	$this->_data = array_merge($this->_data, $data);
    	return $this;
    }
    
    public function load($id) {
    	if (!$this->_resource) return $this;
    	// ...
    	return $this;
    }
    
    public function save() {
    	if (!$this->_resource) return $this;
    	if (!$this->_hasModelChanged()) return $this;
		// ...
    }
    
    public function delete() {
    	if (!$this->_resource || $this->getId()) return $this;
    	// ...
    }
    
}
// END Model Class

/* End of file MY_Model.php */
